﻿using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR;

namespace CsharpTool.SignalRServer
{
    /// <summary>
    /// 消息发送
    /// </summary>
    internal class MessageSend : IMessageSend
    {
        private readonly IHubContext<SignalRHub, ISignalRHubClient> _hubContext;
        public MessageSend(IHubContext<SignalRHub, ISignalRHubClient> hubContext)
        {
            _hubContext = hubContext;
        }

        /// <summary>
        /// <inheritdoc/>
        /// </summary>
        public async Task SendMsgAllAsync<TSendData>(TSendData msg)
        {
            await _hubContext.Clients.All.ReceiveMsgAsync(msg);
        }
        /// <summary>
        /// <inheritdoc/>
        /// </summary>
        public async Task SendMsgAsync<TSendData>(string connectionId, TSendData msg)
        {
            await _hubContext.Clients.Client(connectionId).ReceiveMsgAsync(msg);
        }

        /// <summary>
        /// <inheritdoc/>
        /// </summary>
        public async Task SendMsgAsync<TSendData>(TSendData msg, params string[] connectionIds)
        {
            await _hubContext.Clients.Clients(connectionIds).ReceiveMsgAsync(msg);
        }

        public virtual void Dispose()
        {
        }
    }

    /// <summary>
    /// 消息发送
    /// </summary>
    public class MessageSend<THub, TClient, TReceiveData> : IMessageSend where TClient : class, ISignalRHubClient where THub : Hub<TClient>, ISignalRHub<TClient, TReceiveData>
    {
        public readonly IHubContext<THub, TClient> HubContext;
         
        /// <summary>
        /// ctor
        /// </summary>
        public MessageSend(IHubContext<THub, TClient> hubContext)
        {
            HubContext = hubContext;
        }

        /// <summary>
        /// <inheritdoc/>
        /// </summary>
        public async Task SendMsgAllAsync<TSendData>(TSendData msg)
        {
            await HubContext.Clients.All.ReceiveMsgAsync(msg);
        }
        /// <summary>
        /// <inheritdoc/>
        /// </summary>
        public async Task SendMsgAsync<TSendData>(string connectionId, TSendData msg)
        {
            await HubContext.Clients.Client(connectionId).ReceiveMsgAsync(msg);
        }

        /// <summary>
        /// <inheritdoc/>
        /// </summary>
        public async Task SendMsgAsync<TSendData>(TSendData msg, params string[] connectionIds)
        {
            await HubContext.Clients.Clients(connectionIds).ReceiveMsgAsync(msg);
        }

        public virtual void Dispose()
        {
        }
    }
}
